package chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/** 
 * Contains a straightforward main that implements the client side of a half-duplex (look it up)
 * chat program based on text strings sent over TCP connection between client and server. 
 * 
 * @see EchoServer for server side.
 * @author Lom Hillah
 * @author Justin Templemore
 *
 */
public class EchoClient 
{
	/**
	 * Client logic of the simple chat program.
	 */
	public static void main (String[] args) 
	{
		// local variables for IO
		Socket socketToServer = null;
		PrintWriter outToServer = null;
		BufferedReader inFromServer = null;
		try 
		{
			// local host information (very useful for setting up a conxn)
			InetAddress localhost = InetAddress.getLocalHost();
			System.out.println ("Local host  is " + localhost);
			
			//byte[] serverAddr = { (byte) 192, (byte) 168, 0, 58 };
			//byte[] serverAddr = { (byte) 127, (byte) 0, (byte) 0, (byte) 1 };
			String serverName = "localhost";
					
			// try to open a TCP connection to the server host on port 4444.
			//socketToServer = new Socket(InetAddress.getByAddress(serverAddr), 4444);
			socketToServer = new Socket (InetAddress.getByName(serverName), 4444);
			outToServer = new PrintWriter (socketToServer.getOutputStream(), true);
			inFromServer = new BufferedReader (new InputStreamReader(socketToServer.getInputStream()));
		} 
		catch (UnknownHostException e) 
		{
			System.err.println ("Don't know about specified host");
			System.exit(1);
		} 
		catch (IOException e) 
		{
			System.err.println ("Couldn't get I/O for the connection to the specified host.");
			System.exit(1);
		}

		// Will allow us to read lines of text from the standard input (by default the console)
		BufferedReader inFromConsole = new BufferedReader (new InputStreamReader(System.in));
		String messageToServer, messageFromServer;
		try 
		{
			// until stdIn is closed (returns null)
			while (true) 
			{
				// Read the message to send from the std in (local console)
				System.out.print("> ");
				messageToServer = inFromConsole.readLine();
				System.out.println();

				// write the message on the local socket's output, it will arrive on the remote input
				outToServer.println (messageToServer);
				// read message from remote client. READS ARE BLOCKING so go no further until it is sent and arrives
				messageFromServer = inFromServer.readLine();
				System.out.println("server says: " + messageFromServer);
				if (messageFromServer.equalsIgnoreCase("Bye.")) 
				{
					System.out.println ("Server says goodbye. Quitting.");
					break;
				}
			}
			// close all connection resources.
			outToServer.close();
			inFromServer.close();
			inFromConsole.close();
			socketToServer.close();
		} 
		catch (IOException ioe) 
		{
			System.err.println ("IO connexion to server lost. Quitting.");
			//ioe.printStackTrace();
		}
	}
}
